package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.CouponDetailEntity;
import com.eboss.sbs.model.CouponStoreEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CouponDetailActivity extends BaseActivity {
	
	private int id;
	private CouponDetailEntity myData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_coupon_detail);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "活动详情 ");
		
		id = getIntent().getIntExtra("id", -1);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.layout_stores).setOnClickListener(this);
		
		getdata();
		
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("id", id);
		
		API.http_post(this, API.COUPON_DETAIL, params, new HttpCallback<CouponDetailEntity>(this) {
			@Override
			public void onSuccess(int statusCode, CouponDetailEntity data) {
				
				myData = data;
				
				setTvText(R.id.tv_name, data.getName());
				setTvText(R.id.tv_state, EnumConsts.CouponState.getByState(data.getState()).getName());
				setTvText(R.id.tv_period, data.getPeriod());
				setTvText(R.id.tv_conpon_type, data.getCouponType());
				
				List<CouponStoreEntity> stores = data.getStores();
				int size_stores = stores!=null?stores.size():0;
				setTvText(R.id.tv_stores, size_stores+"家");
				setTvText(R.id.tv_money,  StringUtil.getMoneyString(data.getMoney())+"元");
				setTvText(R.id.tv_limit,  StringUtil.getMoneyString(data.getLimit())+"元");
				setTvText(R.id.tv_facegroup, data.getFacegroup());
				setTvText(R.id.tv_sum, data.getSum()+"张");
				setTvText(R.id.tv_draw, data.getDraw()+"张");
				setTvText(R.id.tv_over, data.getOver()+"张");

			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<CouponDetailEntity>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.layout_stores:
				Intent intent = new Intent();
				intent.setClass(mActivity, CouponShopListAcvitiy.class);
				
				ArrayList<String> storeNames = new ArrayList<String>();
				for (CouponStoreEntity store : myData.getStores()) {
					storeNames.add(store.getName());
				}
				intent.putExtra("names", storeNames);
				startActivity(intent);
				break;
			default:break;
		}
		super.onClick(v);
	}

}
